package com.msf.ui;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileTypeUtil;
import com.msf.ui.fileFilter.SimpleFileFilter;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.io.File;

public class MainFrame extends JFrame {

    private JTextField filePathField;
    private JTextArea textArea;

    private File selectedFile;

    public MainFrame() {
        super();
        try {
            this.initFrame();

            this.setTitle("图片转Base64");

            String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
            UIManager.setLookAndFeel(lookAndFeel);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void initFrame() {
        JPanel topPanel = initTopPanel();
        JPanel centerPanel = initCenterPanel();

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(topPanel, BorderLayout.NORTH);
        panel.add(centerPanel, BorderLayout.CENTER);
        this.setContentPane(panel);
    }

    private JPanel initTopPanel() {
        filePathField = new JTextField();
        filePathField.setEditable(false);

        JButton fileButton = new JButton("选择文件");
        fileButton.addActionListener(e -> { chooseImgFile(); });
        JButton convertButton = new JButton("转换");
        convertButton.addActionListener(e -> { convert2Base64(); });
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(fileButton);
        buttonPanel.add(convertButton);

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(filePathField, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.EAST);
        return panel;
    }

    private JPanel initCenterPanel() {
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JScrollPane(textArea), BorderLayout.CENTER);
        return panel;
    }

    private void chooseImgFile() {
        JFileChooser fileChooser = new JFileChooser();
        String[] imgs = new String[] {"png", "jpg", "jpeg"};
        FileFilter[] fileFilters = fileChooser.getChoosableFileFilters();
        for (FileFilter fileFilter : fileFilters) {
            fileChooser.removeChoosableFileFilter(fileFilter);
        }
        fileChooser.addChoosableFileFilter(new SimpleFileFilter(imgs, "Images(*.png, *.jpg, *.jpeg)"));
        int option = fileChooser.showOpenDialog(this);
        if (option == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            if (file != null) {
                filePathField.setText(file.getAbsolutePath());
                this.selectedFile = file;
            }
        }
    }

    private void convert2Base64() {
        if (selectedFile == null) {
            return;
        }

        String value = Base64.encode(selectedFile);
        String type = FileTypeUtil.getType(selectedFile);
        value = "data:image/" + type + ";base64," + value;
        textArea.setText(value);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            MainFrame mainFrame = new MainFrame();
            mainFrame.setSize(new Dimension(800, 600));
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.setLocationRelativeTo(null);
            mainFrame.setVisible(true);
        });
    }
}
